﻿using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace CKS.EBE.API
{
    internal static class Localization
    {
        private const string DefaultResourceFile = "CKSEBE";
        private const string CoreResourceFile = "core";

        public static IList<string> DocumentLibraries = new List<string>(new string[] { "Media", "Photos", "Themes", "Binaries" });
        public enum BlogList { Posts, Comments, Categories, OtherBlogs, Links, Photos, Media, Themes, Binaries }
        // Original lists resources' keys
        //private const string PostsListName = "posts_schema_blg_title";
        private const string PostsFolderName = "blogpost_Folder";
        //private const string BloglistListName = "bloglist_schema_blg_title";
        private const string BloglistFolderName = "bloglist_Folder";
        //private const string CommentsListName = "comments_schema_blg_title";
        private const string CommentsFolderName = "blogcomments_Folder";
        //private const string LinksListName = "links_schema_blg_title";
        private const string LinksFolderName = "links_Folder";
        //private const string CategoriesListName = "categories_schema_blg_title";
        private const string CategoriesFolderName = "blogcatagories_Folder";
        // CKS lists resources' keys
        private const string ThemesListName = "ListName_Themes";
        private const string MediaListName = "ListName_Media";
        private const string BinariesListName = "ListName_Binaries";
        private static readonly Dictionary<string, string> ListNames = new Dictionary<string, string>();

        /// <summary>
        /// returns the RootFolder name for the list (e.g. Posts)
        /// </summary>
        /// <param name="list"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public static string GetListName(BlogList list, uint language)
        {
            string res = list.ToString();
            string dictionaryKey = string.Format("{0}{1}", res, language);
            // if the dictionary already contains the listname, don't use resources to find it
            if (ListNames.ContainsKey(dictionaryKey)) return ListNames[dictionaryKey];

            switch(list)
            {
                case BlogList.Posts:
                    res = GetResource(PostsFolderName, CoreResourceFile, language);
                    break;
                case BlogList.Comments:
                    res = GetResource(CommentsFolderName, CoreResourceFile, language);
                    break;
                case BlogList.Categories:
                    res = GetResource(CategoriesFolderName, CoreResourceFile, language);
                    break;
                case BlogList.OtherBlogs:
                    res = GetResource(BloglistFolderName, CoreResourceFile, language);
                    break;
                case BlogList.Links:
                    res = GetResource(LinksFolderName, CoreResourceFile, language);
                    break;
                case BlogList.Themes:
                    res = GetResource(ThemesListName, DefaultResourceFile, language);
                    break;
                case BlogList.Binaries:
                    res = GetResource(BinariesListName, DefaultResourceFile, language);
                    break;
                case BlogList.Media:
                    res = GetResource(MediaListName, DefaultResourceFile, language);
                    break;
            }
            // add key to dictionary
            ListNames.Add(dictionaryKey, res);

            return res;
        }

        public static string GetResource(string resKey, string resFile, uint language)
        {
            return SPUtility.GetLocalizedString("$Resources:" + resKey, resFile, language);
        }

        public static string GetResource(string resKey, string resFile)
        {
            // get language from web regionalsettings
            uint language = SPContext.Current.Web.RegionalSettings.LocaleId;
            return GetResource(resKey, resFile, language);
        }

        public static string GetResource(string resKey, uint language)
        {
            return GetResource(resKey, DefaultResourceFile, language);
        }

        public static string GetResource(string resKey)
        {
            // get language from web regionalsettings
            uint languageID = SPContext.Current.Web.RegionalSettings.LocaleId;
            return GetResource(resKey, DefaultResourceFile, languageID);
        }
    }
}
